<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评价管理</el-breadcrumb-item>
      <el-breadcrumb-item>评价图表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-page-header @back="goBack" content="选择商家">
      </el-page-header>
      <div id="main"></div>
    </el-card>
  </div>
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'ShowReport',
  data () {
    return {
      option: {
        title: {
          text: '菜品评价'
        },
        tooltip: {},
        legend: {
          data: ['麻辣', '辣度', '鲜度', '苦度', '咸度', '酸度', '甜度', '评分']
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [
          { name: '麻辣', type: 'bar', data: [] },
          { name: '辣度', type: 'bar', data: [] },
          { name: '鲜度', type: 'bar', data: [] },
          { name: '苦度', type: 'bar', data: [] },
          { name: '咸度', type: 'bar', data: [] },
          { name: '酸度', type: 'bar', data: [] },
          { name: '甜度', type: 'bar', data: [] },
          { name: '评分', type: 'bar', data: [] }
        ]
      }
    }
  },
  // 页面上的元素都渲染完成后，调用该函数
  async mounted () {
    const myChart = echarts.init(document.getElementById('main'))
    const rid = { rid: this.$route.params.rid }
    const { data: res } = await this.$axios.get('/admin/restaurant/queryByID', { params: rid })
    if (res.status !== 2000) {
      return this.$message.error('获取数据失败')
    }
    const nameList = []
    const mspicy = []
    const spicy = []
    const fresh = []
    const bitter = []
    const salt = []
    const sour = []
    const sweet = []
    const commentList = []
    res.data.foods.forEach(function (item) {
      nameList.push(item.fname)
      mspicy.push(item.mspicy)
      spicy.push(item.spicy)
      fresh.push(item.fresh)
      bitter.push(item.bitter)
      salt.push(item.salt)
      sour.push(item.sour)
      sweet.push(item.sweet)
      commentList.push(item.comment)
    })
    this.option.xAxis.data = nameList
    this.option.series[0].data = mspicy
    this.option.series[1].data = spicy
    this.option.series[2].data = fresh
    this.option.series[3].data = bitter
    this.option.series[4].data = salt
    this.option.series[5].data = sour
    this.option.series[6].data = sweet
    this.option.series[7].data = commentList
    myChart.setOption(this.option)
  },
  methods: {
    goBack () {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
#main {
  margin-top: 30px;
  width: 1300px;
  height: 400px;
}
</style>
